package com.test.mchelepesa.activity_fragment_rice.fragment_rice;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.test.mchelepesa.R;
import com.test.mchelepesa.activity_fragment_rice.ServiceFeedbackActivity_rice;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseActivity_rice;
import com.test.mchelepesa.activity_fragment_rice.base_rice.BaseFragment_rice;
import com.test.mchelepesa.adapter_rice.CustomerServiceListAdapter_rice;
import com.test.mchelepesa.bean_rice.config_info_rice.AppShowInfo_rice;
import com.test.mchelepesa.bean_rice.config_info_rice.GlobalConfig_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;

public class PersonalCenterFragment_rice extends BaseFragment_rice {

    private View serviceFeedbackLayout;
    private ListView customerServiceLv;
    private TextView customerServiceDescTv;
    private Button refreshStatusBtn;
    private View termsView;

    private CustomerServiceListAdapter_rice customerServiceListAdapter;
    private BaseActivity_rice activity;

    private int listHeight = 0;

    public boolean toSettingFlag = false;

    public PersonalCenterFragment_rice(){}

    public PersonalCenterFragment_rice(BaseActivity_rice activity){
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal_center_rice, container, false);
        init(view);
        return view;
    }

    private void init(View view){

        serviceFeedbackLayout = view.findViewById(R.id.fragment_personal_center_layout_service_feedback);
        customerServiceLv = view.findViewById(R.id.fragment_personal_center_lv_customer_service);
        customerServiceDescTv = view.findViewById(R.id.fragment_personal_center_tv_customer_service_desc);
        refreshStatusBtn = view.findViewById(R.id.fragment_personal_center_btn_refresh_status);
        termsView = view.findViewById(R.id.fragment_personal_center_view_terms);

        initLicense(
                view.findViewById(R.id.fragment_personal_center_view_license),
                view.findViewById(R.id.fragment_personal_center_license_logo),
                view.findViewById(R.id.fragment_personal_center_license_company_name)
        );

        String hint = AppShowInfo_rice.getInstance().getCustomerHint();
        if (!TextUtils.isEmpty(hint)) customerServiceDescTv.setText(hint);

        serviceFeedbackLayout.setOnClickListener(v -> {
            if (activity!=null) activity.startActivity_rice(ServiceFeedbackActivity_rice.class);
        });

        view.findViewById(R.id.fragment_personal_center_view_setting).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("ME_SETTING_CLICK");
            toSettingFlag = true;
            HttpManager_rice.getInstance().fetchDropConfig();
        });

        refreshStatusBtn.setOnClickListener(v -> HttpManager_rice.getInstance().getAppShowInfo());

        customerServiceListAdapter = new CustomerServiceListAdapter_rice(requireContext());
        customerServiceLv.setAdapter(customerServiceListAdapter);

        final String termOfUserPath = GlobalConfig_rice.getInstance().getTermOfUse();
        if (TextUtils.isEmpty(termOfUserPath)) termsView.setVisibility(View.GONE);
        else {
            termsView.setVisibility(View.VISIBLE);
            termsView.setOnClickListener(v -> {
                HttpManager_rice.getInstance().saveUserBuriedPoint("ME_TERM_OF_USER_CLICK");
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(termOfUserPath)));
            });
        }

        updateCustomerService();

    }

    public void updateCustomerService(){

        customerServiceListAdapter.initCustomerService();
        customerServiceListAdapter.notifyDataSetChanged();

        ViewGroup.LayoutParams layoutParams = customerServiceLv.getLayoutParams();
        if (listHeight==0) listHeight = layoutParams.height;
        layoutParams.height = customerServiceListAdapter.getCount()*listHeight;
        customerServiceLv.setLayoutParams(layoutParams);
    }

}
